//
//  SetViewController.m
//  ChatDemo
//
//  Created by imac on 15/12/20.
//  Copyright © 2015年 imac. All rights reserved.
//

#import "SetViewController.h"

@interface SetViewController ()

- (IBAction)logoutAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (weak, nonatomic) IBOutlet UILabel *name;


@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.btn.layer.cornerRadius = 2;
    self.btn.layer.masksToBounds = YES;
    
    //注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:KNOTIFICATION_LOGINCHANGE
                                               object:nil];
    
    
    NSString *username = [[[EaseMob sharedInstance].chatManager loginInfo] objectForKey:kSDKUsername];
    
    self.name.text = username;
    
}

#pragma mark - login changed
- (void)loginStateChange:(NSNotification *)notification
{
    BOOL isAutoLogin = [[[EaseMob sharedInstance] chatManager] isAutoLoginEnabled];
    BOOL loginSuccess = [notification.object boolValue];
    // 登陆成功加载主窗口控制器
    if (isAutoLogin || loginSuccess) {
        NSString *username = [[[EaseMob sharedInstance].chatManager loginInfo] objectForKey:kSDKUsername];
        self.name.text = username;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)logoutAction
{
    __weak SetViewController *weakSelf = self;
    [self showHudInView:self.view hint:NSLocalizedString(@"setting.logoutOngoing", @"loging out...")];
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
        [weakSelf hideHud];
        if (error && error.errorCode != EMErrorServerNotLogin) {
            [weakSelf showHint:error.description];
        }
        else{
          
            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
        }
    } onQueue:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)logoutAction:(UIButton *)sender {
    [self logoutAction];
}
@end
